use std::collections::LinkedList;

struct User{
    id: u32,
    name: String,
}
impl Drop for User{
    fn drop(&mut self) {
        println!("drop user::{}", self.name);
    }
}
///节点
struct Node<T> {
    data: T,
    
    next: Option<Box<Node<T>>>,
}

pub struct LinkList<T> {
    head: Option<Box<Node<T>>>,
    size: u32
}

impl <T>LinkList<T> {
    pub fn new()->Self{
        LinkList { head:None,size: 0 }
    }

    pub fn size(&self)->u32 {
        self.size
    }
    pub fn is_empty(&self)->bool {
        self.size == 0
    }
    //头部插入
    pub fn push_front(&mut self, data: T)->&mut Self{
        let mut node = Box::new(Node::<T>{data: data, next:None});
        node.next = self.head.take();
        self.head = Some(node);
        self.size += 1;
        self

    }
    //取出头节点
    pub fn pop(&mut self) ->Option<T> {
        if self.is_empty() {
            return None;
        }
        let head = self.head.take();
        let head = head.unwrap();
        self.head = head.next;
        self.size -= 1;
        return Some(head.data);

    }
}

#[test]
pub fn test_linklist(){
    let mut list: LinkList<User> = LinkList::new();
    list.push_front(User{id: 1_u32, name: String::from("张三")})
    .push_front(User{id: 2_u32, name: String::from("李四")});
    let d = list.pop();
    let name = &d.unwrap().name;
    println!("d = {}, list size={}", name, list.size());
}